package api

import (
	"fmt"
	"github.com/LyricTian/gin-admin/v7/internal/app/ginx"
	"github.com/LyricTian/gin-admin/v7/internal/app/schema"
	"github.com/LyricTian/gin-admin/v7/pkg/util/uuid"
	"github.com/LyricTian/gin-admin/v7/pkg/utilx"
	"github.com/gin-gonic/gin"
	"os"
)

func (a *Set) Public(c *gin.Context) {
	var params schema.SetQueryParam
	if err := ginx.ParseQuery(c, &params); err != nil {
		ginx.ResError(c, err)
		return
	}
	params.Pagination = true

	params.Status = 1
	params.Type = 1 //1 表示公开

	result, err := a.SetBll.Query(c.Request.Context(), params)
	if err != nil {
		ginx.ResError(c, err)
		return
	}

	ginx.ResSuccess(c, result.Data.ToMap())
}


func (a *Set) UploadFile(c *gin.Context) {
	f, err := c.FormFile("file")
	if err != nil {
		ginx.ResError(c, fmt.Errorf("文件上传错误:%s", err.Error()))
		return
	}

	path:="upload"

	uploaddir := "www/"+path //文件路径不能是相对路径
	_ = os.Mkdir(uploaddir, 0777)

	// name := util.RandomStr(40)
	name := uuid.NewID()
	ext := utilx.GetTypeByFileName(f.Filename)

	fileName := fmt.Sprintf("%s/%s%s", uploaddir, name, ext)

	err = c.SaveUploadedFile(f, fileName)
	if err != nil {
		ginx.ResError(c, fmt.Errorf("文件保存错误:%s", err.Error()))
		return
	}

	fileName = fmt.Sprintf("%s/%s%s", path, name, ext)


	ginx.ResSuccess(c, schema.UploadFile{FileName: fileName})
}
